package cn.com.alibaba.web.action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import cn.com.alibaba.domain.User;
import cn.com.alibaba.service.UserService;
import cn.com.alibaba.service.impl.UserServiceImpl;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

public class UserAction extends ActionSupport implements ModelDriven<User>{

	private UserService us = new UserServiceImpl(); 
	
	private User user = new User();
	public User getModel() {
		// TODO Auto-generated method stub
		return user;
	}
	
	@InputConfig(resultName="loginInput")
	public String login(){
		
		User loginUser = us.login(user);
		if(loginUser != null){
			System.out.println(loginUser);
			ActionContext.getContext().getSession().put("loginUser", loginUser);
			return "home";
		}else{
			this.addFieldError("", "用户名密码不匹配!");
			return "login";
		}
	}
	public String home(){
		return "home";
	}
	public String getUserList(){
		List<User> list = us.getUserListByCondition(user);
		System.out.println(list);
		ActionContext.getContext().put("userList", list);
		
		return "list";
	}
	
	//上传文件
	private File upload;
	private String uploadFileName;
	private String uploadContentType;
	
	public void setUpload(File upload) {
		this.upload = upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	@InputConfig(resultName="saveInput")
	public String saveUser() throws IOException{
		if(upload != null){
			String fileName = UUID.randomUUID().toString().replace("-", "");
			String dir = ServletActionContext.getServletContext().getRealPath("/WEB-INF/temp");
			FileUtils.copyFile(upload, new File(dir,fileName));
			user.setFilename(uploadFileName);
			user.setPath("/WEB-INF/temp/"+fileName);
		}
		us.saveUser(user);
		return "add";
	}
	
	public String lookUser(){
		User u = us.getUserByUserId(user.getUserID());
		ActionContext.getContext().getValueStack().push(u);
		return "lookUser";
	}
	//编辑
	public String lookUser2(){
		User u = us.getUserByUserId(user.getUserID());
		ActionContext.getContext().getValueStack().push(u);
		return "lookUser2";
	}
	public String deleteUser(){
		us.deleteUserByUserId(user.getUserID());
		return "delete";
	}
	public String updateUser() throws IOException{
		if(upload != null){
			String fileName = UUID.randomUUID().toString().replace("-", "");
			String dir = ServletActionContext.getServletContext().getRealPath("/WEB-INF/temp");
			FileUtils.copyFile(upload, new File(dir,fileName));
			user.setFilename(uploadFileName);
			user.setPath(dir+"/"+fileName);
		}
		us.updateUser(user);
		return "update";
	}
	//下载
	private InputStream target;
	public InputStream getTarget() {
		return target;
	}
	
	private String downloadName;
	public String getDownloadName() throws UnsupportedEncodingException {
		if(downloadName != null){
			return new String(downloadName.getBytes("gbk"),"ISO-8859-1");
		}
		return downloadName;
	}

	public String download(){
		User u = us.getUserByUserId(user.getUserID());
		System.out.println(u.getPath());
		this.target = ServletActionContext.getServletContext().getResourceAsStream(u.getPath());//这个path是从数据库中查询到的
		System.out.println(this.target);
		System.out.println(u.getFilename());
		this.downloadName = u.getFilename();
		return "download";
	}
}
